/**
 * Copyright &copy; 2012-2013 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.oplink.webems.modules.sys.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.oplink.webems.common.persistence.Page;
import com.oplink.webems.modules.sys.dao.DictDao;
import com.oplink.webems.modules.sys.entity.Dict;

/**
 * 字典Service
 * 
 * @author ThinkGem
 * @version 2013-5-29
 */
// Spring Bean的标识.
@Component
// 类中所有public函数都纳入事务管理的标识.
@Transactional
public class DictService {

	private DictDao dictDao;

	public Dict getDict(Long id) {
		return dictDao.findOne(id);
	}

	public void saveDict(Dict entity) {
		dictDao.save(entity);
	}

	public void deleteDict(Long id) {
		dictDao.delete(id);
	}

	public List<Dict> getAllDict() {
		return (List<Dict>) dictDao.findAll();
	}

	public Page<Dict> find(Page<Dict> page, Dict dict) {
		DetachedCriteria dc = dictDao.createDetachedCriteria();
		// ��Ӳ�ѯ����
		if ((dict != null) && StringUtils.isNotEmpty(dict.getType())) {
			dc.add(Restrictions.like("type", "%" + dict.getType() + "%"));
		} else if ((dict != null) && StringUtils.isNotEmpty(dict.getLabel())) {
			dc.add(Restrictions.like("label", "%" + dict.getLabel() + "%"));
		} else if ((dict != null) && StringUtils.isNotEmpty(dict.getValue())) {
			dc.add(Restrictions.like("value", "%" + dict.getValue() + "%"));
		}
		dc.add(Restrictions.eq(Dict.DEL_FLAG, Dict.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("sort"));
		return dictDao.find(page, dc);
	}

	@Autowired
	public void setDictDao(DictDao dictDao) {
		this.dictDao = dictDao;
	}
}
